package com.house.java.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.house.java.dao.DaoFactory;
import com.house.java.dao.SecondHouseDAO;
import com.house.java.dao.impl.SecondHouseDaoImpl;
import com.house.java.model.Paging;
import com.house.java.model.SecondHouse;

public class SecondHouseBusiness {
	private static Logger log = Logger.getLogger(SecondHouseBusiness.class);
	
	/**
	 * 根据查询条件获取符合条件的二手房list
	 * @param maxPrice 最大价格
	 * @param minPrice 最小价格
	 * @param paging 分页数据
	 * @param houseType 户型 : 1、标间，2、一室一厅，3、两室一厅，4、三室一厅，5、三室两厅
	 * @param locationCountryCode 国家CODE 
	 * @param locationProvinceCode	省份CODE
	 * @param locationCityCode	城市CODE
	 * @param locationDistrict	区域CODE
	 * @param houseCategory	住宅类别   1、普通住宅/2、花园洋房 3、写字楼 4、商铺  5、别墅  6、 厂房
	 * @return
	 */
	public List<SecondHouse> querySecondHouses(Integer maxPrice,
			Integer minPrice, Integer houseType,
			Integer locationCountryCode, Integer locationProvinceCode,
			Integer locationCityCode, Integer locationDistrict,
			Integer houseCategory,Integer currentPage,Integer pageSize){
		List<SecondHouse> list = new ArrayList<SecondHouse>();
		SecondHouseDAO dao = (SecondHouseDaoImpl)DaoFactory.getDaoImpl(SecondHouseDAO.class);
		Paging paging = new Paging();
		if(currentPage!=null){
			paging.setCurrentPage(currentPage);
			if(null != pageSize){
				paging.setPageSize(pageSize);
			}else {
				paging.setPageSize(10);
			}
		}else {
			paging.setCurrentPage(1);
			paging.setPageSize(10);
		}
		list = dao.querySecondHouses(maxPrice, minPrice, paging, houseType, locationCountryCode, locationProvinceCode, locationCityCode, locationDistrict, houseCategory);
		return list;
	}
	
	public SecondHouse querySecondHouseById(Long id){
		List<SecondHouse> list = new ArrayList<SecondHouse>();
		SecondHouseDAO dao = (SecondHouseDaoImpl)DaoFactory.getDaoImpl(SecondHouseDAO.class);
		list = dao.queryById(id);
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	
	/**
	 * 添加NewHouse对象
	 * @param house  
	 * @return
	 */
	public boolean addSecondHouse(SecondHouse house){
		if(null == house){
			log.error("the parameter: secondHouse is null! ");
			return false;
		}
		if(null != house.getId()){
			return update(house);
		}else {
			SecondHouseDAO dao = (SecondHouseDaoImpl)DaoFactory.getDaoImpl(SecondHouseDAO.class);
			dao.insertSecondHouse(house);
			if(null != house.getId()){
				return false;
			}
			return false;
		}
	}
	
	
	public boolean update(SecondHouse house){
		if(null != house.getId()){
			SecondHouseDAO dao = (SecondHouseDaoImpl)DaoFactory.getDaoImpl(SecondHouseDAO.class);
			List<SecondHouse> list = dao.queryById(house.getId());
			if(null!=list && list.size() > 0){
				int deleteResult = dao.delete(list.get(0).getId());
				if(deleteResult > 0){
					house.setId(null);
					return addSecondHouse(house);
				}else {
					return false;
				}
			}
			return false;
		}
		return false;
	}
	
	
	public boolean delete(Long id){
		SecondHouseDAO dao = (SecondHouseDaoImpl)DaoFactory.getDaoImpl(SecondHouseDAO.class);
		int result = dao.delete(id);
		if(result > 0){
			return true;
		}else {
			return false;
		}
	}

}
